package com.wfm.leetcode.editor.cn;

/**
 * 跳跃游戏
 * 2025-02-13 10:37:10
 *
 * 假设i，那么i能到达的位置是i+nums[i]
 * 遍历数组中的每个位置，维护最远可以到达的位置，如果当前遍历道德位置x在最远可达范围内，那么我们就更新最远可达位置
 * 时间复杂度O(n)
 * 空间复杂度O(1)
 */
class JumpGame {

    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        public boolean canJump(int[] nums) {
            int n = nums.length;
            int rightmost = 0;
            for (int i = 0; i < n; ++i) {
                if (i <= rightmost) {
                    rightmost = Math.max(rightmost, i + nums[i]);
                    if (rightmost >= n - 1) {
                        return true;
                    }
                }
            }
            return false;
        }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new JumpGame().new Solution();
        
    }
}